Skip to content

Conversation

@AnikethBhosale
Copy link

Description

Implements MSE (Mean Squared Error) metric for Pruna's evaluation framework. The metric computes mean squared error between model predictions and ground truth values, accumulating results across batches using StatefulMetric pattern.

Related Issue

Fixes #388

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Created comprehensive test suite with 15 tests covering:
    • Perfect match scenarios (MSE = 0)
    • Known value calculations
    • Multiple batch accumulation
    • Empty state handling
    • Multi-dimensional tensors (1D to 4D)
    • Device compatibility (CPU/CUDA)
    • Edge cases (None inputs, shape mismatches)
  • All tests pass: 14 passed, 1 skipped (CUDA test on non-CUDA systems)
  • Test coverage: 89% for metric_mse.py
  • Verified style compliance: ty check and ruff check pass

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

  • Metric is registered with MetricRegistry and can be used via Task(metrics=["mse"])
  • Follows existing patterns from other stateful metrics (e.g., SharpnessMetric)
  • Uses list-based state accumulation as required by StatefulMetric framework
  • Documentation includes usage examples, technical details, and related metrics

@begumcig begumcig self-requested a review November 7, 2025 12:43
Copy link
Member

@begumcig begumcig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is already almost flawless, asked for some small changes but it is almost ready to be merged. Thanks a lot @AnikethBhosale

return

# Ensure tensors are on the same device
output_tensor = output_tensor.to(gt_tensor.device)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a great idea, that's why we have integrated device casting in the metric_data_processor. how do you feel about passing the device to it instead?

The model predictions/outputs.
"""
# Process inputs based on call_type (returns tuple of tensors)
inputs = metric_data_processor(x, gt, outputs, self.call_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can pass the device here (regarding the comment below)

@@ -0,0 +1,247 @@
# Copyright 2025 - Pruna AI GmbH. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the variety in the tests! How do you feel about testing with some data from the pruna similar to what we have in tests/evaluation/test_torch_metrics.py?

@@ -0,0 +1,200 @@
# MSE Metric Implementation Summary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you a lot for this detailed summary, are we planning on merging it to Pruna or is it more for giving information? I think this would be even more beneficial as the PR description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add a simple MSE Metric

2 participants